<?php
/**
 * gpa_new Description
 * @package
 * @name controller_admin
 * @filesource controller_admin.php
 * @author Meta Sanjaya
 */

class controller_admin extends Controller
{
    /**
     * @access public
     * @method __construct
     * @param void
     * @return void
     */
    public function  __construct()
    {
        parent::__construct();
    }

    public function control()
    {
        $param = getInstance('uri')->getParam('assoc');
        
        if(!isset($param['app'])) {
            $param['app'] = 'admin';
        }
        
        if(!User::getSession()) {
            redirect(getAppUrl('admin', 'login'), false);
        }

        $user = User::getSession();
        $model = $this->getModel();

        if(!$model->isAdmin($user['username'])) {
            getInstance('session')->set("error_message", "You are not admin!");
            redirect(getAppUrl('admin', 'login'), false);
        }

        switch ($param['app']) {
            case 'admin':
                getInstance('config')->retrieve('global');
                $model = $this->getModel();
                $model_user = $this->getModel('user');
                $view = 'admin_control_html';
                $var['page_title'] = 'Administrator Control';
                $var['config'] = getInstance('config')->getAll();
                $var['groups'] = $model_user->getGroups();
                $var['apps'] = getApps();
                $this->addVars($var);
                $this->renderView($view);
            break;
            default:
                $this->loadAppController($param);
        }
    }

    private function loadAppController($param=array())
    {
        $class = 'admin_' . $param['app'];
        $method = (isset($param['act'])?$param['act']:'__default');
        if(@include_once(getAppPath($param['app']) . $class . '.php')) {
            $this->app = new $class;
            $this->app->$method($param);
        } else {
            die($param['app'] . ' don\'t have admin controller.');
        }
    }

    public function uac($param)
    {
        switch($param[0]) {
            case 'fault':
            default:
                $lang = $this->getLanguage();
                $this->renderView('uac_fault_html');
            break;
        }
    }

    public function login($param)
    {
        $view = 'login_html';
        $var = array();
        if($var['error'] = getInstance("session")->get("error_message")) {
            getInstance("session")->remove("error_message");
        }
        $this->addVars($var);
        $this->renderView($view);
    }

    public function logout() {
        getInstance('session')->destroy();
        redirect(getHttpUrl(), false);
    }

    public function check_login()
    {
        $post = getInstance('request')->post;
        $model = $this->getModel();
        $model_user = $this->getModel('user');
        
        if($model_user->isRegistered($post['username'])) {
            if($model_user->checkPass($post['username'], $post['password'])) {
                if($model->isAdmin($post['username'], $post['password'])) {
                    $return['result'] = true;
                    $user = $model_user->getUserBy('username', $post['username']);
                    $user['isAdmin'] = true;
                    User::setSession($user);
                } else {
                    $return['result'] = false;
                    $return['message'] = "you are not administrator";
                }
            } else {
                $return['result'] = false;
                $return['message'] = "username and password did not match";
            }
        } else {
            $return['result'] = false;
            $return['message'] = "you are not registered";
        }       

        echo json_encode($return);
    }

    public function __default()
    {
        $this->control();
    }
}
?>